<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<meta http-equiv="Cache-Control" content="no-siteapp" />

    <title>C/C++ 构建系统，我用 xmake</title>
    <meta name="description" content="XMake 是什么XMake 是一个基于 Lua 的 现代化 C/C++ 构建系统。它的语法简洁易上手，对新手友好，即使完全不会 lua 也能够快速入门，并且完全无任何依赖，轻量，跨平台。同时，它也是一个自满足的构建系统，拥有强大的包管理系统，快速的构建引擎。相比 Ninja/Scons/Make 作为 Buil...">

    
    <meta name="keywords" content="xmake,lua,C/C++,Buildsystem,tboox" /> 

    <!-- qq oauth -->
    <meta property="qc:admins" content="5211601217706727767255" />

    <!--icon -->
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
    <link rel="icon" sizes="192x192" href="/static/img/nice-highres.png" />
	<link rel="apple-touch-icon-precomposed" href="/static/img/apple-touch-icon-57x57-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="72x72" href="/static/img/apple-touch-icon-72x72-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="114x114" href="/static/img/apple-touch-icon-114x114-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="144x144" href="/static/img/apple-touch-icon-144x144-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="180x180" href="/static/img/retinahd_icon.png" />
	<meta name="msapplication-TileImage" content="/static/img/retinahd_icon.png" />
	
    <link rel="stylesheet" href=" /css/fontawesome/css/font-awesome.min.css ">
    <link rel="stylesheet" href=" /css/main.css ">
    <link rel="canonical" href="https://tboox.org/cn/2021/05/03/please-use-xmake/">
    <link rel="alternate" type="application/rss+xml" title="TBOOX Open Source Project" href="https://tboox.org/feed.xml ">
    <link rel="alternate" hreflang="en" href="https://tboox.org/" />
    <link rel="alternate" hreflang="zh-Hans" href="https://tboox.org/cn/" />

    <!-- css -->
    <link href="/css/reward.css" rel="stylesheet" type="text/css"> 




    <script type="text/javascript">
    function isPC(){    
        var userAgentInfo = navigator.userAgent;  
        var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");    
        var flag = true;    
        for (var v = 0; v < Agents.length; v++) {    
            if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; }    
        }    
        return flag;    
    }
    </script>

<!-- baidu ads -->



    <!-- baidu ads -->

</head>


  <body>

    <header id="top">
    <div class="wrapper">
        <a href="/cn" class="brand">TBOOX</a>
        <button id="headerMenu" class="menu"><i class="fa fa-bars"></i></button>
        <nav id="headerNav">
            <ul>
                <li>
                    
                    <a href="/?lang=0">
                    
                        <i class="fa fa-home"></i>English
                    </a>
                </li>

                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/project/">
                            
                        
                            <i class="fa fa-bookmark"></i>项目
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/archive/">
                            
                        
                            <i class="fa fa-archive"></i>归档
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/category/">
                            
                        
                            <i class="fa fa-th-list"></i>分类
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/tag/">
                            
                        
                            <i class="fa fa-tags"></i>标记
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/docs/">
                            
                        
                            <i class="fa fa-book"></i>文档
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="https://xmake.io/#/zh-cn/about/contact" target="_blank" >
                            
                        
                            <i class="fa fa-forumbee"></i>社区
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/donation/">
                            
                        
                            <i class="fa fa-heart"></i>捐助
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/about/">
                            
                        
                            <i class="fa fa-user"></i>关于
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                    
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            </ul>
        </nav>
    </div>
</header>



        <div class="page clearfix" post>
    <div class="left">
        <h1>C/C++ 构建系统，我用 xmake</h1>
        <div class="label">

            <div class="label-card">
                <i class="fa fa-calendar"></i>2021-05-03
            </div>

            <div class="label-card">
                
            </div>

            <div class="label-card">
                
            </div>

            <div class="label-card">
            


<!-- <span class="point">•</span> -->
<span class="categories">
  <i class="fa fa-th-list"></i>
  
    
        <a href="/category/#xmake" title="Category: xmake" rel="category">xmake</a>
    
  

  <!-- <span class="point">•</span> -->
</span>


            </div>

            <div class="label-card">
            
<!-- <span class="point">•</span> -->
<span class="pageTag">
  <i class="fa fa-tags"></i>
  
    
        <!--a href="/tag/#xmake" title="Tag: xmake" rel="tag">xmake</a-->
        <a href="/cn/tag/#xmake" title="Tag: xmake" rel="tag">xmake</a>&nbsp;
    
        <!--a href="/tag/#lua" title="Tag: lua" rel="tag">lua</a-->
        <a href="/cn/tag/#lua" title="Tag: lua" rel="tag">lua</a>&nbsp;
    
        <!--a href="/tag/#C%2FC%2B%2B" title="Tag: C/C++" rel="tag">C/C++</a-->
        <a href="/cn/tag/#C/C++" title="Tag: C/C++" rel="tag">C/C++</a>&nbsp;
    
        <!--a href="/tag/#Buildsystem" title="Tag: Buildsystem" rel="tag">Buildsystem</a-->
        <a href="/cn/tag/#Buildsystem" title="Tag: Buildsystem" rel="tag">Buildsystem</a>
    
  

</span>

            </div>

        </div>
        <hr>
        <article itemscope itemtype="http://schema.org/BlogPosting">
        <h3 id="xmake-是什么">XMake 是什么</h3>

<p><a href="https://github.com/xmake-io/xmake">XMake</a> 是一个基于 Lua 的 现代化 C/C++ 构建系统。</p>

<p>它的语法简洁易上手，对新手友好，即使完全不会 lua 也能够快速入门，并且完全无任何依赖，轻量，跨平台。</p>

<p>同时，它也是一个自满足的构建系统，拥有强大的包管理系统，快速的构建引擎。</p>

<p>相比 Ninja/Scons/Make 作为 Build backend，CMake/Meson 作为 Project Generator，那么 XMake 就是这两者外加一个包管理。</p>

<div class="highlighter-rouge"><pre class="highlight"><code>xmake = Build backend + Project Generator + Package Manager
</code></pre>
</div>

<p>因此，只需要安装一个不到 3M 的 XMake 安装包，你就可以不用再安装其他各种工具，甚至连 make 都不需要安装，也不需要安装 Python、Java 等重量级的运行时环境，就可以开始您的 C/C++ 开发之旅。</p>

<p>也许，有人会说，编译器总需要安装的吧。这也不是必须的，因为 XMake 的包管理也支持自动远程拉取需要的各种编译工具链，比如：llvm, Mingw, Android NDK 或者交叉编译工具链。</p>

<h3 id="为什么要做-xmake">为什么要做 XMake</h3>

<p>每当在 Reddit 社区跟别人讨论起 XMake，大家总是会拿下面这张图来吐槽。</p>

<p><img src="https://imgs.xkcd.com/comics/standards.png" alt="" /></p>

<p>尽管有些无奈，也被吐槽的有些麻木了，不过我还是想说明下，做 XMake 的初衷，并不是为了分裂 C/C++  生态，相反，XMake 尽可能地复用了现有生态。</p>

<p>同时也让用户在开发 C/C++ 项目的时候，拥有与其他语言一样的良好体验，比如：Rust/Cargo，Nodejs/Npm, Dlang/Dub，不再为到处找第三包，研究如何移植编译而折腾。</p>

<p>因此，如果您还不了解 XMake，请不要过早下定论，可以先尝试使用下，或者花点时间看完下文的详细介绍。</p>

<h3 id="xmake-的特性和优势">XMake 的特性和优势</h3>

<p>经常有人问我 XMake 有什么特别之处，相比现有 CMake、Meson 此类构建工具有什么优势，我为什么要使用 XMake 而不是 CMake？</p>

<p>先说特点和优势，XMake 有以下几点：</p>

<ul>
  <li>简洁易学的配置语法，非 DSL</li>
  <li>强大的包管理，支持语义版本，工具链管理</li>
  <li>足够轻量，无依赖</li>
  <li>极速编译，构建速度和 Ninja 一样快</li>
  <li>简单方便的多平台、工具链切换</li>
  <li>完善的插件系统</li>
  <li>灵活的构建规则</li>
</ul>

<p>至于 CMake，毕竟已成事实上的标准，生态完善，功能强大。</p>

<p>我从没想过让 XMake 去替代它，也替代不了，完全不是一个量级的，但是 CMake 也有许多为人所诟病的短板，比如：语法复杂难懂，包管理支持不完善等等。</p>

<p>因此使用 XMake 可以作为一种补充，对于那些想要简单快速入门 C/C++ 开发的新手，或者想要更加方便易用的包管理，或者想临时快速写一些短小的测试项目。</p>

<p>XMake 都可以帮他们提升开发效率，让其更加关注 C/C++ 项目本身，而不是花更多的时间在构建工具和开发环境上。</p>

<p>下面，我来具体介绍 XMake 的这些主要特性。</p>

<h3 id="语法简洁易上手">语法简洁易上手</h3>

<p>CMake 自己设计一门 DSL 语言用来做项目配置，这对用户来讲提高了学习成本，而且它的语法可读性不是很直观，很容易写出过于复杂的配置脚本，也提高了维护成本。</p>

<p>而 XMake 复用现有知名的 Lua 语言作为基础，并且其上提供了更加简单直接的配置语法。</p>

<p>Lua 本身就是一门简单轻量的胶水语言，关键字和内置类型就那么几种，看个一篇文章，就能基本入门了，并且相比 DSL，能够从网上更方便的获取到大量相关资料和教程。</p>

<h4 id="基础语法">基础语法</h4>

<p>不过，还是有人会吐槽：那不是还得学习 Lua 么？</p>

<p>其实也不用，XMake 采用了 <code class="highlighter-rouge">描述域</code> 和 <code class="highlighter-rouge">脚本域</code> 分离的方式，使得初学者用户在 80% 的情况下，只需要在描述域以更简单直接的方式来配置，完全可以不把它当成 Lua 脚本，例如：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">target</span><span class="p">(</span><span class="s2">"test"</span><span class="p">)</span>
    <span class="n">set_kind</span><span class="p">(</span><span class="s2">"binary"</span><span class="p">)</span>
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"src/*.c"</span><span class="p">)</span>
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"test/*.c"</span><span class="p">,</span> <span class="s2">"example/**.cpp"</span><span class="p">)</span>
</code></pre>
</div>

<p>如果因为，看着有括号，还是像脚本语言的函数调用，那我们也可以这么写（是否带括号看个人喜好，不过我个人还是建议使用上面的方式）</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">target</span> <span class="s2">"test"</span>
    <span class="n">set_kind</span> <span class="s2">"binary"</span>
    <span class="n">add_files</span> <span class="s2">"src/*.c"</span>
    <span class="n">add_files</span> <span class="s2">"test/*.c"</span>
    <span class="n">add_files</span> <span class="s2">"example/**.cpp"</span>
</code></pre>
</div>

<p>我们只需要知道常用配置接口，即使不完全不会 Lua 也能快速配置了。</p>

<p>我们可以对比下 CMake 的配置：</p>

<div class="language-cmake highlighter-rouge"><pre class="highlight"><code><span class="nb">add_executable</span><span class="p">(</span>test <span class="s2">""</span><span class="p">)</span>
<span class="nb">file</span><span class="p">(</span>GLOB SRC_FILES <span class="s2">"src/*.c"</span><span class="p">)</span>
<span class="nb">file</span><span class="p">(</span>GLOB TEST_FILES <span class="s2">"test/*.c"</span><span class="p">)</span>
<span class="nb">file</span><span class="p">(</span>GLOB_RECURSE EXAMPLE_FILES <span class="s2">"example/*.cpp"</span><span class="p">)</span>
<span class="nf">target_sources</span><span class="p">(</span>test PRIVATE
    <span class="si">${</span><span class="nv">SRC_FILES</span><span class="si">}</span>
    <span class="si">${</span><span class="nv">TEST_FILES</span><span class="si">}</span>
    <span class="si">${</span><span class="nv">EXAMPLE_FILES</span><span class="si">}</span>
<span class="p">)</span>
</code></pre>
</div>

<p>哪个更直观可读，一目了然。</p>

<h4 id="条件配置">条件配置</h4>

<p>如果，你已经初步了解了一些 Lua 等基础知识，比如 <code class="highlighter-rouge">if then</code> 等条件判断，那么可以进一步做一些条件配置。</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">target</span><span class="p">(</span><span class="s2">"test"</span><span class="p">)</span>
    <span class="n">set_kind</span><span class="p">(</span><span class="s2">"binary"</span><span class="p">)</span>
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"src/main.c"</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">is_plat</span><span class="p">(</span><span class="s2">"macosx"</span><span class="p">,</span> <span class="s2">"linux"</span><span class="p">)</span> <span class="k">then</span>
        <span class="n">add_defines</span><span class="p">(</span><span class="s2">"TEST1"</span><span class="p">,</span> <span class="s2">"TEST2"</span><span class="p">)</span>
    <span class="k">end</span>
    <span class="k">if</span> <span class="n">is_plat</span><span class="p">(</span><span class="s2">"windows"</span><span class="p">)</span> <span class="ow">and</span> <span class="n">is_mode</span><span class="p">(</span><span class="s2">"release"</span><span class="p">)</span> <span class="k">then</span>
        <span class="n">add_cxflags</span><span class="p">(</span><span class="s2">"-Ox"</span><span class="p">,</span> <span class="s2">"-fp:fast"</span><span class="p">)</span>
    <span class="k">end</span>
</code></pre>
</div>

<p>继续对比下 CMake 版本配置：</p>

<div class="language-cmake highlighter-rouge"><pre class="highlight"><code><span class="nb">add_executable</span><span class="p">(</span>test <span class="s2">""</span><span class="p">)</span>
<span class="nb">if</span> <span class="p">(</span>APPLE OR LINUX<span class="p">)</span>
    <span class="nb">target_compile_definitions</span><span class="p">(</span>test PRIVATE TEST1 TEST2<span class="p">)</span>
<span class="nb">endif</span><span class="p">()</span>
<span class="nb">if</span> <span class="p">(</span>WIN32<span class="p">)</span>
    <span class="nb">target_compile_options</span><span class="p">(</span>test PRIVATE $&lt;$&lt;CONFIG:Release&gt;:-Ox -fp:fast&gt;<span class="p">)</span>
<span class="nb">endif</span><span class="p">()</span>
<span class="nf">target_sources</span><span class="p">(</span>test PRIVATE
    src/main.c
<span class="p">)</span>
</code></pre>
</div>

<h4 id="复杂脚本">复杂脚本</h4>

<p>如果你已经晋升为 XMake 的高端玩家，Lua 语法了然于胸，想要更加灵活的定制化配置需要，并且描述域的几行简单配置已经满足不了你的需求。</p>

<p>那么 XMake 也提供了更加完整的 Lua 脚本定制化能力，你可以写任何复杂的脚本。</p>

<p>比如在构建之前，对所有源文件进行一些预处理，在构建之后，执行外部 gradle 命令进行后期打包，甚至我们还可以重写内部链接规则，实现深度定制编译，我们可以通过<a href="https://xmake.io/#/zh/manual?id=import">import</a> 接口，导入内置的 linker 扩展模块，实现复杂灵活的链接过程。</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">target</span><span class="p">(</span><span class="s2">"test"</span><span class="p">)</span>
    <span class="n">set_kind</span><span class="p">(</span><span class="s2">"binary"</span><span class="p">)</span>
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"src/*.c"</span><span class="p">)</span>
    <span class="n">before_build_file</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">target</span><span class="p">,</span> <span class="n">sourcefile</span><span class="p">)</span>
        <span class="n">io</span><span class="p">.</span><span class="n">replace</span><span class="p">(</span><span class="n">sourcefile</span><span class="p">,</span> <span class="s2">"#define HAVE_XXX 1"</span><span class="p">,</span> <span class="s2">"#define HAVE_XXX 0"</span><span class="p">)</span>
    <span class="k">end</span><span class="p">)</span>
    <span class="n">on_link</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">target</span><span class="p">)</span>
        <span class="n">import</span><span class="p">(</span><span class="s2">"core.tool.linker"</span><span class="p">)</span>
        <span class="n">linker</span><span class="p">.</span><span class="n">link</span><span class="p">(</span><span class="s2">"binary"</span><span class="p">,</span> <span class="s2">"cc"</span><span class="p">,</span> <span class="n">target</span><span class="p">:</span><span class="n">objectfiles</span><span class="p">(),</span> <span class="n">target</span><span class="p">:</span><span class="n">targetfile</span><span class="p">(),</span> <span class="p">{</span><span class="n">target</span> <span class="o">=</span> <span class="n">target</span><span class="p">})</span>
    <span class="k">end</span><span class="p">)</span>
    <span class="n">after_build</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">target</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">is_plat</span><span class="p">(</span><span class="s2">"android"</span> <span class="k">then</span>
            <span class="n">os</span><span class="p">.</span><span class="n">cd</span><span class="p">(</span><span class="s2">"android/app"</span><span class="p">)</span>
            <span class="n">os</span><span class="p">.</span><span class="n">exec</span><span class="p">(</span><span class="s2">"./gradlew app:assembleDebug"</span><span class="p">)</span>
        <span class="k">end</span>
    <span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

<p>如果换成 CMake，也可以 add_custom_command 里面实现，不过里面似乎只能简单的执行一些批处理命令，没法做各种复杂的逻辑判断，模块加载，自定义配置脚本等等。</p>

<p>当然，使用 cmake 肯定也能实现上面描述的功能，但绝对不会那么简单。</p>

<p>如果有熟悉 cmake 的人，也可以尝试帮忙完成下面的配置：</p>

<div class="language-cmake highlighter-rouge"><pre class="highlight"><code><span class="nb">add_executable</span><span class="p">(</span>test <span class="s2">""</span><span class="p">)</span>
<span class="nb">file</span><span class="p">(</span>GLOB SRC_FILES <span class="s2">"src/*.c"</span><span class="p">)</span>
<span class="nb">add_custom_command</span><span class="p">(</span>TARGET test PRE_BUILD
    -- TODO
    COMMAND echo hello
<span class="p">)</span>
<span class="nb">add_custom_command</span><span class="p">(</span>TARGET test POST_BUILD
    COMMAND cd android/app
    COMMAND ./gradlew app:assembleDebug
<span class="p">)</span>
-- How can we override link stage?
<span class="nf">target_sources</span><span class="p">(</span>test PRIVATE
    <span class="si">${</span><span class="nv">SRC_FILES</span><span class="si">}</span>
<span class="p">)</span>
</code></pre>
</div>

<h3 id="强大的包管理">强大的包管理</h3>

<p>众所周知，做 C/C++ 相关项目开发，最头大的就是各种依赖包的集成，由于没有像 Rust/Cargo 那样完善的包管理系统。</p>

<p>因此，我们每次想使用一个第三方库，都需要各种找，研究各种平台的移植编译，还经常遇到各种编译问题，极大耽误了开发者时间，无法集中精力去投入到实际的项目开发中去。</p>

<p>好不容易当前平台搞定了，换到其他平台，有需要重新折腾一遍依赖包，为了解决这个问题，出现了一些第三方的包管理器，比如 vcpkg/conan/conda等等，但有些不支持语义版本，有些支持的平台有限，但不管怎样，总算是为解决 C/C++ 库的依赖管理迈进了很大一步。</p>

<p>但是，光有包管理器，C/C++ 项目中使用它们还是比较麻烦，因为还需要对应构建工具能够很好的对其进行集成支持才行。</p>

<h4 id="cmake-和-vcpkg">CMake 和 Vcpkg</h4>

<p>我们先来看下 CMake 和 Vcpkg 的集成支持：</p>

<div class="highlighter-rouge"><pre class="highlight"><code>cmake_minimum_required(VERSION 3.0)
project(test)
find_package(unofficial-sqlite3 CONFIG REQUIRED)
add_executable(main main.cpp)
target_link_libraries(main PRIVATE unofficial::sqlite3::sqlite3)
</code></pre>
</div>

<p>缺点：</p>

<ul>
  <li>还需要额外配置 <code class="highlighter-rouge">-DCMAKE_TOOLCHAIN_FILE=&lt;vcpkg_dir&gt;/scripts/buildsystems/vcpkg.cmake"</code></li>
  <li>不支持自动安装依赖包，还需要用户手动执行 <code class="highlighter-rouge">vcpkg install xxx</code> 命令安装</li>
  <li>vcpkg 的语义版本选择不支持 （据说新版本开始支持了）</li>
</ul>

<h4 id="cmake-和-conan">CMake 和 Conan</h4>

<div class="highlighter-rouge"><pre class="highlight"><code>```cmake
cmake_minimum_required(VERSION 2.8.12)
project(Hello)

add_definitions("-std=c++11")

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

add_executable(hello hello.cpp)
target_link_libraries(hello gtest)
</code></pre>
</div>

<p>conanfile.txt</p>

<div class="language-text highlighter-rouge"><pre class="highlight"><code>[requires]
gtest/1.10.0

[generators]
cmake
</code></pre>
</div>

<p>缺点：</p>

<ul>
  <li>同样，还是需要额外调用 <code class="highlighter-rouge">conan install ..</code> 来安装包</li>
  <li>还需要额外配置一个 conanfile.txt 文件去描述包依赖规则</li>
</ul>

<h4 id="meson-和-vcpkg">Meson 和 Vcpkg</h4>

<p>我没找到如何在 Meson 中去使用 vcpkg 包，仅仅找到一篇相关的 <a href="https://github.com/mesonbuild/meson/issues/3500">Issue #3500</a> 讨论。</p>

<h4 id="meson-和-conan">Meson 和 Conan</h4>

<p>Meson 似乎还没有对 Conan 进行支持，但是 Conan 官方文档上有解决方案，对齐进行支持，但是很复杂，我是没看会，大家可以自行研究：<a href="https://docs.conan.io/en/latest/reference/build_helpers/meson.html">https://docs.conan.io/en/latest/reference/build_helpers/meson.html</a></p>

<h4 id="xmake-和-vcpkg">XMake 和 Vcpkg</h4>

<p>前面讲了这么多，其他构建工具和包管理的集成，个人感觉用起来很麻烦，而且不同的包管理器，集成方式差别很大，用户想要快速从 Vcpkg 切换到 Conan 包，改动量非常大。</p>

<p>接下来，我们来看看 XMake 中集成使用 Vcpkg 提供的包：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_requires</span><span class="p">(</span><span class="s2">"vcpkg::zlib"</span><span class="p">,</span> <span class="p">{</span><span class="n">alias</span> <span class="o">=</span> <span class="s2">"zlib"</span><span class="p">})</span>
<span class="n">target</span><span class="p">(</span><span class="s2">"test"</span><span class="p">)</span>
    <span class="n">set_kind</span><span class="p">(</span><span class="s2">"binary"</span><span class="p">)</span>
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"src/*.c"</span><span class="p">)</span>
    <span class="n">add_packages</span><span class="p">(</span><span class="s2">"zlib"</span><span class="p">)</span>
</code></pre>
</div>

<p>我们只需要通过 <code class="highlighter-rouge">add_requires</code> 配置上对应的包名，以及 <code class="highlighter-rouge">vcpkg::</code> 包命名空间，就能直接集成使用 vcpkg 提供的 zlib 包。</p>

<p>然后，我们只需要执行 xmake 命令，既可完成整个编译过程，包括 zlib 包的自动安装，无需额外手动执行 <code class="highlighter-rouge">vcpkg install zlib</code>。</p>

<div class="language-bash highlighter-rouge"><pre class="highlight"><code><span class="gp">$ </span>xmake
note: try installing these packages <span class="o">(</span>pass -y to skip confirm<span class="o">)</span>?
<span class="gp">-&gt; </span>vcpkg::zlib
please input: y <span class="o">(</span>y/n<span class="o">)</span>

<span class="gp">=&gt; </span>install vcpkg::zlib .. ok
<span class="o">[</span> 25%]: compiling.release src<span class="se">\m</span>ain.cpp
<span class="o">[</span> 50%]: linking.release <span class="nb">test</span>
<span class="o">[</span>100%]: build ok!
</code></pre>
</div>

<h4 id="xmake-和-conan">XMake 和 Conan</h4>

<p>接下来是集成 Conan 的包，完全一样的方式，仅仅执行换个包管理器名字。</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_requires</span><span class="p">(</span><span class="s2">"conan::zlib"</span><span class="p">,</span> <span class="p">{</span><span class="n">alias</span> <span class="o">=</span> <span class="s2">"zlib"</span><span class="p">})</span>
<span class="n">target</span><span class="p">(</span><span class="s2">"test"</span><span class="p">)</span>
    <span class="n">set_kind</span><span class="p">(</span><span class="s2">"binary"</span><span class="p">)</span>
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"src/*.c"</span><span class="p">)</span>
    <span class="n">add_packages</span><span class="p">(</span><span class="s2">"zlib"</span><span class="p">)</span>
</code></pre>
</div>

<p>XMake 同样会自动安装 conan 中的 zlib 包，然后自动集成编译。</p>

<h4 id="xmake-自建包管理">XMake 自建包管理</h4>

<p>XMake 跟 CMake 还有其他构建系统，最大的不同点，也就是最大的优势之一，就是它有完全自建的包管理系统，我们完全可以不依赖 vcpkg/conan，也可以快速集成依赖包，比如：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_requires</span><span class="p">(</span><span class="s2">"zlib 1.2.x"</span><span class="p">,</span> <span class="s2">"tbox &gt;= 1.6.0"</span><span class="p">)</span>
<span class="n">target</span><span class="p">(</span><span class="s2">"test"</span><span class="p">)</span>
    <span class="n">set_kind</span><span class="p">(</span><span class="s2">"binary"</span><span class="p">)</span>
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"src/*.c"</span><span class="p">)</span>
    <span class="n">add_packages</span><span class="p">(</span><span class="s2">"zlib"</span><span class="p">,</span> <span class="s2">"tbox"</span><span class="p">)</span>
</code></pre>
</div>

<p>而且，它还支持完整的语义版本选择，多平台的包集成，交叉编译工具链的包集成，甚至编译工具链包的自动拉取使用。</p>

<p>不仅如此，我们开可以对定制化配置对自建包的依赖，例如：</p>

<h5 id="使用调式版本依赖包">使用调式版本依赖包</h5>

<p>我们可以使用 debug 版本库，实现对依赖库的断点调试。</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_requires</span><span class="p">(</span><span class="s2">"zlib"</span><span class="p">,</span> <span class="p">{</span><span class="n">debug</span> <span class="o">=</span> <span class="kc">true</span><span class="p">})</span>
</code></pre>
</div>

<h5 id="设置-msvc-运行时库">设置 msvc 运行时库</h5>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_requires</span><span class="p">(</span><span class="s2">"zlib"</span><span class="p">,</span> <span class="p">{</span><span class="n">configs</span> <span class="o">=</span> <span class="p">{</span><span class="n">vs_runtime</span> <span class="o">=</span> <span class="s2">"MD"</span><span class="p">}})</span>
</code></pre>
</div>

<h5 id="使用动态库">使用动态库</h5>

<p>默认集成的是静态库，我们也可以切换到动态库。</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_requires</span><span class="p">(</span><span class="s2">"zlib"</span><span class="p">,</span> <span class="p">{</span><span class="n">configs</span> <span class="o">=</span> <span class="p">{</span><span class="n">shared</span> <span class="o">=</span> <span class="kc">true</span><span class="p">}})</span>
</code></pre>
</div>

<h5 id="语义版本支持">语义版本支持</h5>

<p>XMake 的自建包集成支持完整的版本语义规范。</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_requires</span><span class="p">(</span><span class="s2">"zlib 1.2.x"</span><span class="p">)</span>
<span class="n">add_requires</span><span class="p">(</span><span class="s2">"zlib &gt;=1.2.10"</span><span class="p">)</span>
<span class="n">add_requires</span><span class="p">(</span><span class="s2">"zlib ~1.2.0"</span><span class="p">)</span>
</code></pre>
</div>

<h5 id="禁止使用系统库">禁止使用系统库</h5>

<p>默认情况下，如果版本匹配，XMake 会优先查找使用系统上用户已经安装的库，当然我们也可以强制禁止查找使用系统库，仅仅从自建包仓库中下载安装包。</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_requires</span><span class="p">(</span><span class="s2">"zlib"</span><span class="p">,</span> <span class="p">{</span><span class="n">system</span> <span class="o">=</span> <span class="kc">true</span><span class="p">})</span>
</code></pre>
</div>

<h5 id="可选依赖包">可选依赖包</h5>

<p>如果依赖包集成失败，XMake 会自动报错，中断编译，提示用户：<code class="highlighter-rouge">zlib not found</code>，但是我们也可以设置为可选包集成，这样的话，即使库最终没安装成功，也不影响项目的编译，仅仅只是跳过这个依赖。</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_requires</span><span class="p">(</span><span class="s2">"zlib"</span><span class="p">,</span> <span class="p">{</span><span class="n">optional</span> <span class="o">=</span> <span class="kc">true</span><span class="p">})</span>
</code></pre>
</div>

<h5 id="包的定制化配置">包的定制化配置</h5>

<p>比如，集成使用开启了 context/coroutine 模块配置的 boost 库。</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_requires</span><span class="p">(</span><span class="s2">"boost"</span><span class="p">,</span> <span class="p">{</span><span class="n">configs</span> <span class="o">=</span> <span class="p">{</span><span class="n">context</span> <span class="o">=</span> <span class="kc">true</span><span class="p">,</span> <span class="n">coroutine</span> <span class="o">=</span> <span class="kc">true</span><span class="p">}})</span>
</code></pre>
</div>

<h4 id="支持的包管理仓库">支持的包管理仓库</h4>

<p>XMake 除了支持 vcpkg/conan 还有自建仓库的包集成支持，还支持其他的包管理仓库，例如：Conda/Homebrew/Apt/Pacman/Clib/Dub 等等，而且集成方式完全一致。</p>

<p>用户可与快速切换使用其他的仓库包，而不需要花太多时间去研究如何集成它们。</p>

<p>因此，XMake 并没有破坏 C/C++ 生态，而是极大的复用现有 C/C++ 生态的基础上，努力改进用户对 C/C++ 依赖包的使用体验，提高开发效率，让用户能够拥有更多的时间去关注项目本身。</p>

<ul>
  <li>官方自建仓库 <a href="https://github.com/xmake-io/xmake-repo">xmake-repo</a> (tbox &gt;1.6.1)</li>
  <li>官方包管理器 <a href="https://github.com/xmake-io/xrepo">Xrepo</a></li>
  <li><a href="https://xmake.io/#/zh-cn/package/remote_package?id=%e4%bd%bf%e7%94%a8%e8%87%aa%e5%bb%ba%e7%a7%81%e6%9c%89%e5%8c%85%e4%bb%93%e5%ba%93">用户自建仓库</a></li>
  <li>Conan (conan::openssl/1.1.1g)</li>
  <li>Conda (conda::libpng 1.3.67)</li>
  <li>Vcpkg (vcpkg:ffmpeg)</li>
  <li>Homebrew/Linuxbrew (brew::pcre2/libpcre2-8)</li>
  <li>Pacman on archlinux/msys2 (pacman::libcurl)</li>
  <li>Apt on ubuntu/debian (apt::zlib1g-dev)</li>
  <li>Clib (clib::clibs/bytes@0.0.4)</li>
  <li>Dub (dub::log 0.4.3)</li>
</ul>

<h4 id="独立的包管理命令xrepo">独立的包管理命令（Xrepo）</h4>

<p>为了方便 XMake 的自建仓库中的包管理，以及第三方包的管理使用，我们也提供了独立的 Xrepo cli 命令工具，来方便的管理我们的依赖包</p>

<p>我们可以使用这个工具，快速方便的完成下面的管理操作：</p>

<ul>
  <li>安装包：<code class="highlighter-rouge">xrepo install zlib</code></li>
  <li>卸载包：<code class="highlighter-rouge">xrepo remove zlib</code></li>
  <li>获取包信息：<code class="highlighter-rouge">xrepo info zlib</code></li>
  <li>获取包编译链接 flags：<code class="highlighter-rouge">xrepo fetch zlib</code></li>
  <li>加载包虚拟 Shell 环境：<code class="highlighter-rouge">xrepo env shell</code> （这是一个很强大的特性）</li>
</ul>

<p>我们可以到 <a href="https://github.com/xmake-io/xrepo">Xrepo 项目主页</a> 查看更多的介绍和使用方式。</p>

<p><img src="https://xrepo.xmake.io/assets/img/xrepo.gif" alt="" /></p>

<h3 id="轻量无依赖">轻量无依赖</h3>

<p>使用 Meson/Scons 需要先安装 python/pip，使用 Bazel 需要先安装 java 等运行时环境，而 XMake 不需要额外安装任何依赖库和环境，自身安装包仅仅2-3M，非常的轻量。</p>

<p>尽管 XMake 是基于 lua，但是借助于 lua 胶水语言的轻量级特性，xmake 已将其完全内置，因此安装完 XMake 等同于拥有了一个完整的 lua vm。</p>

<p>有人会说，编译工具链总还是需要的吧，也不完全是，Windows 上，我们提供了预编译安装包，可以直接下载安装编译，地址见：<a href="https://github.com/xmake-io/xmake/releases">Releases</a></p>

<p>另外，XMake 还支持远程拉取编译工具链，因此即使你的系统环境，还没有安装任何编译器，也没关系，用户完全不用考虑如何折腾编译环境，只需要在 xmake.lua 里面配置上需要的工具链即可。</p>

<p>比如，我们在 Windows 上使用 mingw-w64 工具链来编译 C/C++ 工程，只需要做如下配置即可。</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_requires</span><span class="p">(</span><span class="s2">"mingw-w64"</span><span class="p">)</span>
<span class="n">target</span><span class="p">(</span><span class="s2">"test"</span><span class="p">)</span>
    <span class="n">set_kind</span><span class="p">(</span><span class="s2">"binary"</span><span class="p">)</span>
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"src/*.c"</span><span class="p">)</span>
    <span class="n">set_toolchains</span><span class="p">(</span><span class="s2">"mingw@mingw-w64"</span><span class="p">)</span>
</code></pre>
</div>

<p>通过 <code class="highlighter-rouge">set_toolchains</code> 配置绑定 mingw-w64 工具链包后，XMake 就会自动检测当前系统是否存在 mingw-64，如果还没安装，它会自动下载安装，然后完成项目编译，整个过程，用户仅仅只需要执行 <code class="highlighter-rouge">xmake</code> 这个命令就能完成。</p>

<div class="language-bash highlighter-rouge"><pre class="highlight"><code><span class="gp">$ </span>xmake
note: try installing these packages <span class="o">(</span>pass -y to skip confirm<span class="o">)</span>?
<span class="k">in </span>xmake-repo:
<span class="gp">-&gt; </span>mingw-w64 8.1.0 <span class="o">[</span>vs_runtime:MT]
please input: y <span class="o">(</span>y/n<span class="o">)</span>

<span class="gp">=&gt; </span>download https://jaist.dl.sourceforge.net/project/mingw-w64/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/8.1.0/threads-posix/seh/x86_64-8.1.0-release-posix-seh-rt_v6-rev0.7z .. ok
checking <span class="k">for </span>mingw directory ... C:<span class="se">\U</span>sers<span class="se">\r</span>uki<span class="se">\A</span>ppData<span class="se">\L</span>ocal<span class="se">\.</span>xmake<span class="se">\p</span>ackages<span class="se">\m\m</span>ingw-w64<span class="se">\8</span>.1.0<span class="se">\a</span>ad6257977e0449595004d7441358fc5
<span class="o">[</span> 25%]: compiling.release src<span class="se">\m</span>ain.cpp
<span class="o">[</span> 50%]: linking.release test.exe
<span class="o">[</span>100%]: build ok!
</code></pre>
</div>

<p>除了 mingw-w64，我们还可以配置远程拉取使用其他的工具链，甚至交叉编译工具链，例如：llvm-mingw, llvm, tinycc, muslcc, gnu-rm, zig 等等。</p>

<p>如果大家还想进一步了解远程工具链的拉取集成，可以看下文档：<a href="https://xmake.io/#/zh-cn/package/remote_package?id=%e8%87%aa%e5%8a%a8%e6%8b%89%e5%8f%96%e8%bf%9c%e7%a8%8b%e5%b7%a5%e5%85%b7%e9%93%be">自动拉取远程工具链</a>。</p>

<h3 id="极速并行编译">极速并行编译</h3>

<p>大家都知道 Ninja 构建非常快，因此很多人都喜欢用 CMake/Meson 生成 build.ninja  后，使用 Ninja 来满足极速构建的需求。</p>

<p>尽管 Ninja 很快，但是我们还是需要先通过 meson.build 和 CMakelist.txt 文件生成 build.ninja 才行，这个生成过程也会占用几秒甚至十几秒的时间。</p>

<p>而 XMake 不仅仅拥有和 Ninja 近乎相同的构建速度，而且不需要额外再生成其他构建文件，直接内置构建系统，任何情况下，只需要一个 <code class="highlighter-rouge">xmake</code> 命令就可以实现极速编译。</p>

<p>我们也做过一些对比测试数据，供大家参考：</p>

<h4 id="多任务并行编译测试">多任务并行编译测试</h4>

<table>
  <thead>
    <tr>
      <th>构建系统</th>
      <th>Termux (8core/-j12)</th>
      <th>构建系统</th>
      <th>MacOS (8core/-j12)</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>xmake</td>
      <td>24.890s</td>
      <td>xmake</td>
      <td>12.264s</td>
    </tr>
    <tr>
      <td>ninja</td>
      <td>25.682s</td>
      <td>ninja</td>
      <td>11.327s</td>
    </tr>
    <tr>
      <td>cmake(gen+make)</td>
      <td>5.416s+28.473s</td>
      <td>cmake(gen+make)</td>
      <td>1.203s+14.030s</td>
    </tr>
    <tr>
      <td>cmake(gen+ninja)</td>
      <td>4.458s+24.842s</td>
      <td>cmake(gen+ninja)</td>
      <td>0.988s+11.644s</td>
    </tr>
  </tbody>
</table>

<h4 id="单任务编译测试">单任务编译测试</h4>

<table>
  <thead>
    <tr>
      <th>构建系统</th>
      <th>Termux (-j1)</th>
      <th>构建系统</th>
      <th>MacOS (-j1)</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>xmake</td>
      <td>1m57.707s</td>
      <td>xmake</td>
      <td>39.937s</td>
    </tr>
    <tr>
      <td>ninja</td>
      <td>1m52.845s</td>
      <td>ninja</td>
      <td>38.995s</td>
    </tr>
    <tr>
      <td>cmake(gen+make)</td>
      <td>5.416s+2m10.539s</td>
      <td>cmake(gen+make)</td>
      <td>1.203s+41.737s</td>
    </tr>
    <tr>
      <td>cmake(gen+ninja)</td>
      <td>4.458s+1m54.868s</td>
      <td>cmake(gen+ninja)</td>
      <td>0.988s+38.022s</td>
    </tr>
  </tbody>
</table>

<h3 id="傻瓜式多平台编译">傻瓜式多平台编译</h3>

<p>XMake 的另外一个特点，就是高效简单的多平台编译，不管你是编译 windows/linux/macOS 下的程序，还是编译 iphoneos/android 又或者是交叉编译。</p>

<p>编译的配置方式大同小异，不必让用户去这折腾研究各个平台下如何去编译。</p>

<p><img src="https://xmake.io/assets/img/index/xmake-basic-render.gif" alt="" /></p>

<h4 id="编译本机-windowslinuxmacos--程序">编译本机 Windows/Linux/MacOS  程序</h4>

<p>当前本机程序编译，我们仅仅只需要执行：</p>

<div class="language-bash highlighter-rouge"><pre class="highlight"><code><span class="gp">$ </span>xmake
</code></pre>
</div>

<p>对比 CMake</p>

<div class="language-bash highlighter-rouge"><pre class="highlight"><code><span class="gp">$ </span>mkdir build
<span class="gp">$ </span><span class="nb">cd </span>build
<span class="gp">$ </span>cmake --build ..
</code></pre>
</div>

<h4 id="编译-android-程序">编译 Android 程序</h4>

<div class="language-bash highlighter-rouge"><pre class="highlight"><code><span class="gp">$ </span>xmake f -p android --ndk<span class="o">=</span>~/android-ndk-r21e
<span class="gp">$ </span>xmake
</code></pre>
</div>

<p>对比 CMake</p>

<div class="language-bash highlighter-rouge"><pre class="highlight"><code><span class="gp">$ </span>mkdir build
<span class="gp">$ </span><span class="nb">cd </span>build
<span class="gp">$ </span>cmake -DCMAKE_TOOLCHAIN_FILE<span class="o">=</span>~/android-ndk-r21e/build/cmake/android.toolchain.cmake ..
<span class="gp">$ </span>make
</code></pre>
</div>

<h4 id="编译-ios-程序">编译 iOS 程序</h4>

<div class="language-bash highlighter-rouge"><pre class="highlight"><code><span class="gp">$ </span>xmake f -p iphoneos
<span class="gp">$ </span>xmake
</code></pre>
</div>

<p>对比 CMake</p>

<div class="language-bash highlighter-rouge"><pre class="highlight"><code><span class="gp">$ </span>mkdir build
<span class="gp">$ </span><span class="nb">cd </span>build
<span class="gp">$ </span>wget https://raw.githubusercontent.com/leetal/ios-cmake/master/ios.toolchain.cmake
<span class="gp">$ </span>cmake -DCMAKE_TOOLCHAIN_FILE<span class="o">=</span><span class="sb">`</span><span class="nb">pwd</span><span class="sb">`</span>/ios.toolchain.cmake ..
<span class="gp">$ </span>make
</code></pre>
</div>

<p>我没有找到很方便的方式去配置编译 ios 程序，仅仅只能从其他地方找到一个第三方的 ios 工具链去配置编译。</p>

<h4 id="交叉编译">交叉编译</h4>

<p>我们通常只需要设置交叉编译工具链根目录，XMake 会自动检测工具链结构，提取里面的编译器参与编译，不需要额外配置什么。</p>

<div class="language-bash highlighter-rouge"><pre class="highlight"><code><span class="gp">$ </span>xmake f -p cross --sdk<span class="o">=</span>~/aarch64-linux-musl-cross
<span class="gp">$ </span>xmake
</code></pre>
</div>

<p>对比 CMake</p>

<p>我们需要先额外写一个 cross-toolchain.cmake 的交叉工具链配置文件。</p>

<div class="language-cmake highlighter-rouge"><pre class="highlight"><code><span class="nb">set</span><span class="p">(</span>CMAKE_SYSTEM_NAME Linux<span class="p">)</span>
<span class="nb">set</span><span class="p">(</span>CMAKE_SYSTEM_PROCESSOR aarch64<span class="p">)</span>

<span class="nb">set</span><span class="p">(</span>TOOL_CHAIN_DIR ~/aarch64-linux-musl<span class="p">)</span>
<span class="nb">set</span><span class="p">(</span>TOOL_CHAIN_INCLUDE <span class="si">${</span><span class="nv">TOOL_CHAIN_DIR</span><span class="si">}</span>/aarch64-linux-musl/include<span class="p">)</span>
<span class="nb">set</span><span class="p">(</span>TOOL_CHAIN_LIB <span class="si">${</span><span class="nv">TOOL_CHAIN_DIR</span><span class="si">}</span>/aarch64-linux-musl/lib<span class="p">)</span>

<span class="nb">set</span><span class="p">(</span>CMAKE_C_COMPILER <span class="s2">"aarch64-linux-gcc"</span><span class="p">)</span>
<span class="nb">set</span><span class="p">(</span>CMAKE_CXX_COMPILER <span class="s2">"aarch64-linux-g++"</span><span class="p">)</span>

<span class="nb">set</span><span class="p">(</span>CMAKE_FIND_ROOT_PATH <span class="si">${</span><span class="nv">TOOL_CHAIN_DIR</span><span class="si">}</span>/aarch64-linux-musl<span class="p">)</span>

<span class="nb">set</span><span class="p">(</span>CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER<span class="p">)</span>
<span class="nb">set</span><span class="p">(</span>CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY<span class="p">)</span>
<span class="nb">set</span><span class="p">(</span>CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY<span class="p">)</span>
<span class="nb">set</span><span class="p">(</span>CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY<span class="p">)</span>

<span class="nb">include_directories</span><span class="p">(</span><span class="si">${</span><span class="nv">TOOL_CHAIN_DIR</span><span class="si">}</span>/aarch64-linux-musl/include<span class="p">)</span>
<span class="nb">set</span><span class="p">(</span>CMAKE_INCLUDE_PATH <span class="si">${</span><span class="nv">TOOL_CHAIN_INCLUDE</span><span class="si">}</span><span class="p">)</span>
<span class="nb">set</span><span class="p">(</span>CMAKE_LIBRARY_PATH <span class="si">${</span><span class="nv">TOOL_CHAIN_LIB</span><span class="si">}</span><span class="p">)</span>
</code></pre>
</div>

<div class="language-bash highlighter-rouge"><pre class="highlight"><code><span class="gp">$ </span>mkdir build
<span class="gp">$ </span><span class="nb">cd </span>build
<span class="gp">$ </span>cmake -DCMAKE_TOOLCHAIN_FILE<span class="o">=</span>../cross-toolchain.cmake ..
<span class="gp">$ </span>make
</code></pre>
</div>

<h3 id="结语">结语</h3>

<p>如果你是 C/C++ 开发的新手，可以通过 XMake 快速上手入门 C/C++ 编译构建。</p>

<p>如果你想开发维护跨平台 C/C++ 项目，也可以考虑使用 XMake 来维护构建，提高开发效率，让你更加专注于项目本身，不再为折腾移植依赖库而烦恼。</p>

<p>欢迎关注 XMake 项目：</p>

<ul>
  <li><a href="https://github.com/xmake-io/xmake/">Github 项目地址</a></li>
  <li><a href="https://xmake.io/#/">项目主页</a></li>
  <li><a href="https://github.com/xmake-io/xmake-repo">XMake 包管理仓库</a></li>
  <li>社区
    <ul>
      <li><a href="https://t.me/tbooxorg">Telegram 群组</a></li>
      <li><a href="https://discord.gg/xmake">Discord 聊天室</a></li>
      <li>QQ 群：343118190, 662147501</li>
      <li>微信公众号：tboox-os</li>
    </ul>
  </li>
  <li>课程：<a href="https://xmake.io/#/zh-cn/about/course">Xmake 带你轻松构建 C/C++ 项目</a></li>
  <li>活动：<a href="https://tboox.org/cn/2021/04/29/xmake-summer-ospp/">开源之夏 &amp; Xmake</a></li>
</ul>


        </article>
        <hr>

        <!-- baidu ads -->
        

        <!-- reward -->
        <div style="text-align: center;">
            <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
              <span>赏</span>
            </button>
            <div id="QR" style="display: none;">
                <div id="wechat" style="display: inline-block">
                  <img id="wechat_qr" src="/static/img/weixin.png" alt="WeChat Pay"/>
                  <p>微信打赏</p>
                </div>
                <div id="alipay" style="display: inline-block">
                  <img id="alipay_qr" src="/static/img/alipay.png" alt="Alipay"/>
                  <p>支付宝打赏</p>
                </div>
            </div>
        </div>

        
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
                    
                    <h2 id="english">English</h2>
                    <ul>
                    
                    <li class="relatedPost">
                        <a href="/2021/05/03/please-use-xmake/">C/C++ build system, I use xmake
                        
                        </a>
                    </li>
                    
                    
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
        
            </ul>
        

        
        
            
        
            
            
            
                
                    
                        
                        <h2 id="similar_posts">相关文章</h2>
                        <ul>
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/12/17/xmake-update-v2.6.2/">xmake v2.6.2 发布，新增 Linux 内核驱动模块构建支持
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/12/03/xmake-update-v2.6.1/">xmake v2.6.1 发布，使用 Lua5.4 运行时，Rust 和 C++ 混合编译支持
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/10/30/xmake-update-v2.5.9/">xmake v2.5.9 发布，改进 C++20 模块，并支持 Nim, Keil MDK 和 Unity Build
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/10/08/xmake-update-v2.5.8/">xmake v2.5.8 发布，新增 Pascal/Swig 程序和 Lua53 运行时支持
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/08/29/xmake-update-v2.5.7/">xmake v2.5.7 发布，包依赖锁定和 Vala/Metal 语言编译支持
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
            
        
        
            </ul>
        

        <div class="post-recent">
    <div class="pre">

        

        

        
        
        <p><strong>上一篇</strong> <a href="/cn/2021/04/29/xmake-summer-ospp/">Summer2021：暑期来 Xmake 社区做项目吧，还有奖金领取哦</a></p>
        
    </div>

    <div class="nex">

        

        

        
        
        

        

        

        
        
        <p><strong>下一篇</strong> <a href="/cn/2021/05/15/xmake-update-v2.5.4/">xmake v2.5.4 发布，支持 apt/portage 包管理器，改进 xrepo shell 环境</a></p>
        
    </div>
</div>


        <h2 id="comments">评论</h2>
        






<div id="gitalk-container"></div>
<link rel="stylesheet" href="/css/gitalk.css">
<script src="/js/gitalk.min.js"></script>

<script>
const gitalk = new Gitalk({
  clientID: '73946dc1d9e2276ad0da',
  clientSecret: '12a3cb94361ba3ebc6ecb68cf80d592bfaa8106d',
  repo: 'tboox.github.io',
  owner: 'waruqi',
  admin: ['waruqi'],
  id: location.pathname,       
  language: 'zh-CN',
  distractionFreeMode: false  
})

gitalk.render('gitalk-container')
</script>





    </div>
    <button class="anchor"><i class="fa fa-anchor"></i></button>
    <div class="right">
        <div class="wrap">

            <!-- codefund ads -->
            

            <!-- Content -->
            <div class="side content">
                <div>
                    内容
                </div>
                <ul id="content-side" class="content-ul">
                    <li><a href="#english">English</a></li>
                    <li><a href="#similar_posts">相关文章</a></li>
                    <li><a href="#comments">评论</a></li>
                </ul>
            </div>


            <!-- baidu ads -->
            
            
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    链接
                </div>
                <ul class="content-ul">
                  <li><a href="http://github.com/waruqi/tbox">tbox</a></li>
                  <li><a href="http://www.xmake.io">xmake</a></li>
                  <li><a href="https://github.com/waruqi">github</a></li>
                </ul>
            </div> 

            <!-- xmake courses -->
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    xmake 入门课程
                </div>
                <a href="https://xmake.io/#/zh-cn/about/course" target="_blank">
                <img src="/static/img/xmake-course.png" alt="course" width="256" height="193">
                </a>
            </div>

            <!-- qqgroup -->
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    技术交流群（QQ）
                </div>
                <img src="/static/img/qqgroup.png" alt="qqgroup" width="256" height="284">
            </div> 

            <!-- google ads -->
            

            <!-- baidu ads -->
            

        </div>
    </div>

    <!-- baidu ads -->
    
</div>
<script>
/**
 * target _blank
 */
(function() {
    var aTags = document.querySelectorAll('article a:not([id])')
    for (var i = 0; i < aTags.length; i++) {
        aTags[i].setAttribute('target', '_blank')
    }
}());
</script>
<script src="/js/pageContent.js " charset="utf-8"></script>



    <footer class="site-footer">
    <div class="wrapper">
        <p class="description">
             Copyright (c) 2016-2020 tboox.org 
        </p>
        <p class="contact">
            
            <a href="https://github.com/waruqi" title="GitHub"><i class="fa fa-github" aria-hidden="true"></i></a> 
             
            
            <a href="mailto:waruqi@gmail.com" title="email"><i class="fa fa-envelope-o" aria-hidden="true"></i></a> 
            
            
            <a href="https://twitter.com/waruqi" title="Twitter"><i class="fa fa-twitter" aria-hidden="true"></i></a> 
            
            <a href="/feed.xml" title="feed"><i class="fa fa-feed" aria-hidden="true"></i></a> 
        </p>
        <p class="power">
            <span>
                Site powered by <a href="https://jekyllrb.com/">Jekyll</a> & <a href="https://github.com/Gaohaoyang">HyG</a> & <a href="https://pages.github.com/">Github Pages</a>.
            </span>
        </p>
    </div>
</footer>

    <div class="back-to-top">
    <a href="#top" class="scroll">
        <i class="fa fa-arrow-up" aria-hidden="true"></i>
    </a>
</div>

    <script src=" /js/main.js " charset="utf-8"></script>
    <script src=" /js/scroll.min.js " charset="utf-8"></script>
  </body>

</html>
